package cn.cjlmonster.demo02.widget;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.lifecycle.LifecycleOwner;
import androidx.lifecycle.MediatorLiveData;
import androidx.lifecycle.Observer;

import com.chad.library.adapter.base.entity.MultiItemEntity;

import cn.cjlmonster.demo02.R;
import cn.cjlmonster.demo02.virtual.dev.GatewayDevice;
import cn.cjlmonster.demo02.virtual.dev.HotPumpDevice;
import cn.cjlmonster.demo02.virtual.dev.NewAirDevice;
import cn.cjlmonster.demo02.virtual.dev.ThermostatDevice;
import cn.cjlmonster.demo02.virtual.dev.ThermostatDevices;
import cn.cjlmonster.demo02.virtual.dev.WaterHeaterDevice;

/**
 * cn.cjlmonster.demo02.widget
 *
 * @author cjl
 * @project Demo02
 * @date 2022/11/4 22:23
 */
public class RandomText extends LinearLayout implements Observer<Integer> {


    private static final String TAG = "RandomText123";

    private final TextView tv_random;
    private final TextView tv_name;

    public RandomText(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        LayoutInflater.from(context).inflate(R.layout.widget_random_text, this);
        setOrientation(VERTICAL);
        tv_random = findViewById(R.id.tv_random);
        tv_name = findViewById(R.id.tv_name);
    }

    public void setRandom(int randomNumber) {
        tv_random.setText(String.format("%s", randomNumber));
    }

    public void setName(String name) {
        tv_name.setText(name);
    }

    @Override
    public void onChanged(Integer randomNumber) {
        if (waterHeaterDevice != null)
        Log.i("RandomText1234", "onChanged: randomNumber = " + randomNumber + ", " + waterHeaterDevice.name);
        setRandom(randomNumber);
    }

    public void setDevice(LifecycleOwner owner, MultiItemEntity item) {
        switch (item.getItemType()) {
            case 0:
                setGatewayDevice(owner, (GatewayDevice) item);
                break;
            case 1:
                setHotPumpDevice(owner, (HotPumpDevice) item);
                break;
            case 2:
                setWaterHeaterDevice(owner, (WaterHeaterDevice) item);
                break;
            case 3:
                setNewAirDevice(owner, (NewAirDevice) item);
                break;
            case 5:
                setThermostatDevices(owner, (ThermostatDevices) item);
                break;
        }
    }

    private GatewayDevice gatewayDevice;

    public void setGatewayDevice(LifecycleOwner owner, GatewayDevice device) {
        if (gatewayDevice != null) {
            gatewayDevice.unsubscribe(owner, this);
        }

        device.unsubscribe(owner);

        gatewayDevice = device;
        gatewayDevice.subscribe(owner, this);
        setRandom(gatewayDevice.randomNumber);
        setName(gatewayDevice.name);
    }

    private HotPumpDevice hotPumpDevice;

    public void setHotPumpDevice(LifecycleOwner owner, HotPumpDevice device) {
        if (hotPumpDevice != null) {
            hotPumpDevice.unsubscribe(owner, this);
        }

        device.unsubscribe(owner);

        hotPumpDevice = device;
        hotPumpDevice.subscribe(owner, this);
        setRandom(hotPumpDevice.randomNumber);
        setName(hotPumpDevice.name);
    }

    private WaterHeaterDevice waterHeaterDevice;

    public void setWaterHeaterDevice(LifecycleOwner owner, WaterHeaterDevice device) {
        if (waterHeaterDevice != null) {
            Log.i(TAG, ": " + device.name + ", " + hashCode() + ", " + waterHeaterDevice.hashCode() + ", " + device.hashCode());
            waterHeaterDevice.unsubscribe(owner, this);
        } else {
            Log.i(TAG, ": " + device.name + ", " + hashCode() + ", null, " + device.hashCode());
        }

        device.unsubscribe(owner);

        waterHeaterDevice = device;
        waterHeaterDevice.subscribe(owner, this);
        setRandom(waterHeaterDevice.randomNumber);
        setName(waterHeaterDevice.name);
    }

    private NewAirDevice newAirDevice;

    public void setNewAirDevice(LifecycleOwner owner, NewAirDevice device) {
        if (newAirDevice != null) {
            newAirDevice.unsubscribe(owner, this);
        }

        device.unsubscribe(owner);

        newAirDevice = device;
        newAirDevice.subscribe(owner, this);
        setRandom(newAirDevice.randomNumber);
        setName(newAirDevice.name);
    }

    private ThermostatDevices thermostatDevices;

    private MediatorLiveData<Integer> liveData;

    public void setThermostatDevices(LifecycleOwner owner, ThermostatDevices devices) {
        if (thermostatDevices != null && thermostatDevices.hashCode() == devices.hashCode()) return;
        thermostatDevices = devices;
        setName("温控器集合");
        if (liveData == null) liveData = new MediatorLiveData<>();
        for (ThermostatDevice dev : thermostatDevices.thermostatDevices) {
            owner.getLifecycle().addObserver(dev);
            liveData.addSource(dev.getRandomLiveData(), integer -> liveData.postValue(integer));
        }
        liveData.observe(owner, this);
    }
}
